<?php
/**
 * Formular hostingu
 * @author Tomas Srna
 * @package bakalarka
 */

class HostingSettingsForm extends Form
{
	public function __construct(Template $template, $values = NULL, 
		$action = NULL, $method = 'POST')
	{
		parent::__construct($template, $values, $action, $method);
		
		// name, label, [type], [class], [hint], [$options], [maxlen]
		$this->items = array(
			new Element('documentroot', L::selected()->documentroot, "text", 
						"required nospace path", L::selected()->documentrootHint, NULL, 100),
			new Element('adminmail', L::selected()->adminmail, "text", 
						"required email"),
			new Element('php', L::selected()->php, "select", 
						"required", "", array(0 => L::selected()->disable,
							1 => L::selected()->enable)),
			new Element('ssl', L::selected()->ssl, "select", 
						"required", "", array(0 => L::selected()->disable,
							1 => L::selected()->enable)),
			new Element('subdomaindir', L::selected()->subdomaindir, "text", 
						"", L::selected()->subdomaindirHint),
			new Element('symlinks', L::selected()->symlinks, "select", 
						"required", "", array(0 => L::selected()->disable,
							1 => L::selected()->enable)),
			new Element('indexes', L::selected()->indexes, "select", 
						"required", "", array(0 => L::selected()->disable,
							1 => L::selected()->enable)),
			new Element('allowoverride', L::selected()->allowoverride, "select", 
						"required", L::selected()->allowoverrideHint, 
							array(0 => L::selected()->disable,
							1 => L::selected()->enable)),
		);
	}

	/**
	 * Pokial nam zakladne kontroly nestacia, mozme si pridat vlastne
	 * specificke pre dany formular
	 */
	public function validate()
	{
		parent::validate();
		
		if(!$this->getError('documentroot'))
		{
			$parts = explode("/", $this->getValue('documentroot'));
			if(array_search('..', $parts) !== false)
			{
				$this->setError('documentroot', 
					sprintf(L::selected()->invalidFormat_sn, 
					'..'));
			}
		}
		
		// Nesmie zacinat /
		if(!$this->getError('documentroot') && 
			substr($this->getValue('documentroot'), 0, 1) == '/')
		{
			$this->setError('documentroot',
				L::selected()->noSlashBegin);
		}
		
		if(count($this->errors) > 0)
			return false;
		
		return true;
	}
}
 